package com.briup.cms.controller;

import com.briup.cms.util.Result;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
@Api(tags = "文件上傳")
public class UPloadFileController {

    @Autowired
    private FastFileStorageClient storageClient;

    @Value("${image.access.path}")
    private String basePath;

    @PostMapping("/singlefile")
    @ApiOperation(value = "上傳單個文件")
    public Result upload(@RequestParam("userFile") MultipartFile file) throws IOException {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), suffix, null);
        return Result.success(basePath + storePath.getFullPath());
    }
}
